﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System.Collections.Generic;
using System.Linq;

#endregion

namespace AzureMRCore.Utils
{
    public class JSONUtils
    {
        public static Dictionary<string, object> parseJSON(string jsonString)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            if (jsonString != null)
            {
                jsonString = jsonString.Trim();
                //if (jsonString.StartsWith("{")){
                //    jsonString = jsonString.Substring(1);
                //}
                //if(jsonString.EndsWith("}")){
                //    jsonString = jsonString.Remove(jsonString.Length-1);
                //}
                jsonString = jsonString.Trim('}', '{');
                string[] pairs = jsonString.Split(new[] {','});
                //.ToDictionary(y=>y.Substring(0,(y.IndexOf(':')-1)), y=>y.Substring(y.IndexOf(':')));
                foreach (string pair in pairs)
                {
                    if (pair.Contains(':'))
                    {
                        string key = pair.Substring(0, pair.IndexOf(':'));
                        key = key.Trim('"');
                        string value = pair.Substring(pair.IndexOf(':') + 1);
                        value = value.Trim('"');

                        dictionary.Add(key, value);
                    }
                }
            }
            return dictionary;
        }

        public static string ToJson(IDictionary<string, object> bag)
        {
            string json = ToPartialJson(bag);
            if (json.LastIndexOf(',') == json.Length)
            {
                json = json.TrimEnd(',');
            }
            json += "}";

            return json;
        }

        public static string ToJsonFromPartial(string partialJsonString, string mapID, string blobUrl)
        {
            string json = partialJsonString + "\"" + Constants.MAPID + "\":\"" + mapID + "\"," + "\"" +
                          Constants.BLOB_URI + "\":\"" + blobUrl + "\"}";
            return json;
        }

        public static string ToPartialJson(IDictionary<string, object> bag)
        {
            return bag.Aggregate("{", (current, entry) => current + ("\"" + entry.Key + "\":\"" + entry.Value + "\","));
        }
    }
}